/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_WRITER_H
#define RTPS_WRITER_H

#include "rtps/ThreadPool.h"
#include "rtps/discovery/TopicData.h"
#include "rtps/entities/ReaderProxy.h"
#include "rtps/entities/WriterProxy.h"
#include "rtps/storages/CacheChange.h"
#include "rtps/storages/MemoryPool.h"
#include "rtps/storages/PBufWrapper.h"

typedef struct rtps_Writer {
  TopicData m_attributes;
  bool m_is_initialized;
  bool m_bstateful;  /*default is false, stateless*/
  struct rtps_MemoryPool *m_proxies;/*<ReaderProxy, NUM_READER_PROXIES_PER_WRITER>*/
} Writer;
void rtps_Writer_Construct(struct rtps_Writer *writer);

#define VIRTUAL

VIRTUAL bool rtps_Writer_addNewMatchedReader(const ReaderProxy *newProxy);
VIRTUAL void rtps_Writer_removeReader(const Guid_t *guid);
VIRTUAL void rtps_Writer_removeReaderOfParticipant(const GuidPrefix_t *guidPrefix);

//! Executes required steps like sending packets. Intended to be called by
//! worker threads
VIRTUAL void rtps_Writer_progress();
VIRTUAL const struct rtps_CacheChange *newChange(ChangeKind_t kind, const uint8_t *data,
                                       DataSize_t size);
VIRTUAL void rtps_Writer_setAllChangesToUnsent();
VIRTUAL void rtps_Writer_onNewAckNack(const SubmessageAckNack *msg,
                            const GuidPrefix_t *sourceGuidPrefix);

VIRTUAL void rtps_Writer_destruct(struct rtps_Writer *writer);

bool rtps_Writer_isInitialized(struct rtps_Writer *writer);
#endif // RTPS_WRITER_H
